public class DefaultHttpRequestAuthImpl implements IHttpRequestAuth {

    private RedisStorageImpl storage;
    //构造方法区
    public DefaultHttpRequestAuthImpl() {
        storage = new RedisStorageImpl();
    }
    public DefaultHttpRequestAuthImpl(RedisStorageImpl Storage ) {
        this.storage = Storage;
    }

    //方法区
    @Override
    public boolean auth(String baseRequest) {
        HttpRequest Request = HttpRequest.ParseFromUrl(baseRequest);
        return auth(Request);
    }

    @Override
    public boolean auth(HttpRequest httpRequest) {
        String appId = httpRequest.getAppId();
        String token = httpRequest.getToken();
        long timestamp = httpRequest.getTimestamp();
        String originalUrl = httpRequest.getBaseUrl();
        AuthToken clientAuthToken = new AuthToken(token, timestamp);
        if (clientAuthToken.isExpired()) {
            return false;
        }
        String password = storage.getPasswordByAppId(appId);
        AuthToken serverAuthToken = AuthToken.generate(originalUrl, appId, timestamp, password);
        if (!serverAuthToken.match(clientAuthToken)) {
            return false;
        }
        return true;
    }
}
